import uploadFileToOss from "../../lib/uploadAliyun.js";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        uploadData:{},//上传数据
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    chooseVideo() {
        var that = this
        wx.chooseVideo({
            sourceType: ['album', 'camera'],
            maxDuration: 60,
            camera: 'back',
            success: (res) => {
                that.setData({
                    src: res.tempFilePath
                });
                const fileNameArr = res.tempFilePath.split(/\.|\//);
                const objectId = fileNameArr[fileNameArr.length-2];
                let begin = Date.now();
                this.setData({
                    uploadData:{}
                })
                if(res.size > 350 * 1024 * 1024) {
                    wx.showModal({
                        title: '文件大小超出限制350M',
                        content:`${res.size}`,
                        showCancel: false,
                    })
                    return;
                }
                console.log("size",res.size);
                uploadFileToOss({
                    filePath:res.tempFilePath,
                    dir:"test",
                    objectId,
                    success: (res)=>{
                        console.log("上传成功",res);
                        let end = Date.now();
                        wx.showModal({
                            title: '上传时间',
                            content:`${end - begin}`,
                            showCancel: false,
                        })
                    },
                    error:(e) => {
                        console.log("上传失败",e);
                    },
                    onProgress:(res) => {
                        console.log(res);
                        this.setData({
                            uploadData:res
                        })
                    }
                });
            }
        })
    },

    upload() {
        let key = "test";
        this.client.multipartUpload(key, this.tempFilePath, {
            progress: (p) => {
                return (done) => {
                    console.log(Math.floor(p * 100) + '%');
                    done();
                }
            }
        }).then(function (res) {
            console.log('upload success: %j', res);
        })
    }


})

/**
 * 公司网络wifi
 * 手机上传1:10视频
 * 大小:13.966M
 * 上传耗时8.3s
 */